package com.gengzp.permission.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.gengzp.permission.model.UserPrincipalModel;

/**
 * @ClassName UserPrincipalModelThreadLocal
 * @Description 用户认证线程变量上下文
 * 使用阿里封装的TransmittableThreadLocal, 对ThreadLocal进行扩展, 使其支持父子线程的传递
 * @Author gengzp
 * @Date 2025/8/14 23:50
 */
public class UserPrincipalContextHolder {

    private static final ThreadLocal<UserPrincipalModel> userPrincipalModelThreadLocal = new TransmittableThreadLocal<>();

    /**
     * 从线程变量上下文中获取获取当前用户认证信息
     *
     * @return 用户认证信息
     */
    public static UserPrincipalModel get() {
        return userPrincipalModelThreadLocal.get();
    }

    /**
     * 将用户认证信息保存到线程变量上下文
     *
     * @param userPrincipalModel 用户认证信息
     */
    public static void set(UserPrincipalModel userPrincipalModel) {
        userPrincipalModelThreadLocal.set(userPrincipalModel);
    }

    /**
     * 清除用户认证信息线程变量上下文
     */
    public static void clear() {
        userPrincipalModelThreadLocal.remove();
    }

}
